package zabbix

import (
	"encoding/json"
	"errors"
)

func ActionCreateData() (string,[]byte) {
	method := "action.create"
	// 创建一个将发现的主机链接到模板 10001
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Discovery action",
        "eventsource": 1,
        "status": 0,
        "esc_period": "0s",
        "filter": {
            "evaltype": 0,
            "conditions": [
                {
                    "conditiontype": 21,
                    "value": "1"
                },
                {
                    "conditiontype": 10,
                    "value": "2"
                }
            ]
        },
        "operations": [
            {
                "esc_step_from": 1,
                "esc_period": "0s",
                "optemplate": [
                    {
                        "templateid": "10091"
                    }
                ],
                "operationtype": 6,
                "esc_step_to": 1
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,jsonstr
}

func ActionDeleteData() (string,map[string]interface{}) {
	method := "action.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "17",
        "18"
    ]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ActionGetData() (string,map[string]interface{}) {
	method := "action.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
		"output": "extend",
        "selectOperations": "extend",
        "selectRecoveryOperations": "extend",
        "selectFilter": "extend",
        "filter": {
            "eventsource": 1
        }
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ActionUpdateData() (string,map[string]interface{}) {
	method := "action.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
		"actionid": "2",
        "status": "1"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ActionCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ActionCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ActionDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ActionDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ActionGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ActionGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ActionUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ActionUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

